package com.the_ring.chapter13.server;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.the_ring.chapter13.bean.ImageMessage;
import com.the_ring.chapter13.bean.JoinInfo;
import com.the_ring.chapter13.bean.MessageInfo;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class WechatServer {

    // 客户端映射表
    private static Map<String, SocketIOClient> clientMap = new HashMap<>();

    // 人员名字映射表
    private static Map<String, String> nameMap = new HashMap<>();

    // 群名称与群成员映射表
    private static Map<String, Map<String, String>> groupMap = new HashMap<>();

    public static void main(String[] args) {
        Configuration config = new Configuration();
        config.setPort(9010);       // 设置监听端口
        final SocketIOServer server = new SocketIOServer(config);

        // 添加我已上线的监听事件
        server.addEventListener("self_online", String.class, ((client, data, ackSender) -> {
            String sessionId = client.getSessionId().toString();
            for (Map.Entry<String, SocketIOClient> item : clientMap.entrySet()) {
                item.getValue().sendEvent("friend_online", data);
                client.sendEvent("friend_online", nameMap.get(item.getKey()));
            }
            nameMap.put(sessionId, data);
        }));

        // 添加我已下线的监听事件
        server.addEventListener("self_offline", String.class, ((client, data, ackSender) -> {
            String sessionId = client.getSessionId().toString();
            for (Map.Entry<String, SocketIOClient> item : clientMap.entrySet()) {
                if (!sessionId.equals(item.getKey())) {
                    item.getValue().sendEvent("friend_offline", data);
                }
            }
            nameMap.remove(sessionId);
        }));

        // 添加文本发送的时间监听器
        server.addEventListener("send_friend_message", String.class, ((client, data, ackSender) -> {
            MessageInfo message = new Gson().fromJson(data, MessageInfo.class);
            for (Map.Entry<String, String> item : nameMap.entrySet()) {
                if (message.getTo().equals(item.getValue())) {
                    clientMap.get(item.getKey()).sendEvent("receive_friend_message", message);
                    break;
                }
            }
        }));

        // 添加图像信息的事件监听器
        server.addEventListener("send_friend_image", String.class, ((client, data, ackSender) -> {
            ImageMessage message = new Gson().fromJson(data, ImageMessage.class);
            for (Map.Entry<String, String> item : nameMap.entrySet()) {
                if (message.getTo().equals(item.getValue())) {
                    clientMap.get(item.getKey()).sendEvent("receive_friend_message", message);
                    break;
                }
            }
        }));
        
        // 添加入群的事件监听器
        server.addEventListener("join_group", String.class, ((client, data, ackSender) -> {
            String sessionId = client.getSessionId().toString();
            JoinInfo info = new Gson().fromJson(data, JoinInfo.class);
            if (!groupMap.containsKey(info.getGroupName())) {
                groupMap.put(info.getGroupName(), new HashMap<>());
            }
            Map<String, String> group = groupMap.get(info.getGroupName());
            group.put(sessionId, info.getUserName());
            for (Map.Entry<String, String> user : group.entrySet()) {
                clientMap.get(user.getKey()).sendEvent("person_in_group", info.getUserName());
            }
            client.sendEvent("person_count", group.size());
        }));

        // 添加退群事件监听
        server.addEventListener("leave_group", String.class, ((client, data, ackSender) -> {
            String sessionId = client.getSessionId().toString();
            JoinInfo info = new Gson().fromJson(data, JoinInfo.class);
            Map<String, String> group = groupMap.get(info.getGroupName());
            group.remove(sessionId);
            for (Map.Entry<String, String> user : group.entrySet()) {
                clientMap.get(user.getKey()).sendEvent("person_out_group", info.getUserName());
            }
        }));

        // 添加群消息发送的事件监听器
        server.addEventListener("send_group_message", String.class, ((client, data, ackSender) -> {
            MessageInfo message = new Gson().fromJson(data, MessageInfo.class);
            Map<String, String> group = groupMap.get(message.getTo());
            for (Map.Entry<String, String> user : group.entrySet()) {
                if (!user.getValue().equals(message.getFrom())) {
                    clientMap.get(user.getKey()).sendEvent("receive_group_message", message);
                }
            }
        }));

        // 添加群图片发送的事件监听器
        server.addEventListener("send_group_image", String.class, ((client, data, ackSender) -> {
            ImageMessage message = new Gson().fromJson(data, ImageMessage.class);
            Map<String, String> group = groupMap.get(message.getTo());
            for (Map.Entry<String, String> user : group.entrySet()) {
                if (!user.getValue().equals(message.getFrom())) {
                    clientMap.get(user.getKey()).sendEvent("receive_group_image", message);
                }
            }
        }));


    }
}
